<?php
class base extends Action{
	protected $_G = array();

	/**
	 * 对数据进行处理
	 *
	 */
	protected function init(){
		/*if (isset($_GET)) $_GET = $this->_htmlspecialchars($_GET);
		if (isset($_POST)) $_POST = $this->_htmlspecialchars($_POST);
		if (isset($_REQUEST)) $_REQUEST = $this->_htmlspecialchars($_REQUEST);
		if (isset($_COOKIE)) $_COOKIE = $this->_htmlspecialchars($_COOKIE);*/
	}
	//语言包
	protected function lang() {
		//语言包初始化
		$config['LANG_DEFAULT'] = 'error';
		//默认语言
		$config['LANG_PACK_PATH'] = './lang/';
		//语言包目录
		$config['LANG_PACK_SUFFIX'] = '.lang.php';
		//语言包后缀
		$config['LANG_PACK_COMMON'] = 'common';
		//公用语言包，默认会自动
		lang::init($config);
	}

	public function __construct(){
		if(!isset($_SESSION)){
			session_start();
		}
		
		define('__TPL__',__ROOT__.'/template/api');
		$this -> _G['timestamp'] = time();
		//$this->init();
		//$this -> check_login();
		//登录验证

		
		//初始化语言包
		$this -> lang();
		
		$cookie = cookie('member');
		$this->_G['member'] = cp_decode($cookie,cpConfig::get('authkey'));//用户数据		
		
		$this -> assign('_G', $this->_G);
	}

	/**
	 * 验证登录状态
	 *
	 */
	protected function check_login(){
		$member = array();
		$cookie = cookie('member');
		if(empty($cookie)) $cookie = $_POST['cookie'];//防止修改头像时获取不到cookie
		$db = model();
		$member = cp_decode($cookie,cpConfig::get('authkey'));
		
		
		if(isset($member['user_agent']) && $member['user_agent']==user_agent()){//检测cookie有效性，防止用户盗用cookie
			$find['uid'] = $member['uid'];
			$find['password'] = $member['password'];
			$have = $db->table('member')->where($find)->find();
		}
		if($have){
			unset($have['salt']);
			$this->_G['member'] = $have;
		}else{
			cookie('member',null);
			$this->_G['member'] = Array(
			'uid' => 0,
			'usergroup' => 9
			);
		}		
	}
	/**
	 * 验证权限
	 *
	 * @return unknown
	 */
	protected function check_auth(){
		$config['AUTH_GROUP']=$this->_G['member']['usergroup'];
		$config['AUTH_ADMIN']=1;
		$config['AUTH_NO_CHECK']=array(
			'admin'=>array(
				'login'=>'*',		
			),		
		);
		RBAC::init($config);
		return RBAC::check();
	}
	/**
	 * 采用htmlspecialchars反转义特殊字符
	 *
	 * @param array|string $data 待反转义的数据
	 * @return array|string 反转义之后的数据
	 */
	private function _htmlspecialchars(&$data) {
		return is_array($data) ? array_map(array($this, '_htmlspecialchars'), $data) : trim ( htmlspecialchars ( $data ) );
	}
	protected function textin($data){
		return is_array($data) ? array_map(array($this, 'textin'), $data) : CPstr::s_text($data);
	}
	protected function htmlin($data){
		return is_array($data) ? array_map(array($this, 'htmlin'), $data) : CPstr::s_html($data);
	}

	/**
	 * 文件上传
	 *
	 */
	protected function _upfile() {
		$upload = new UploadFile();
		// 实例化上传类
		$upload -> maxSize = 3145728;
		// 设置附件上传大小
		$upload -> allowExts = array('jpg', 'gif', 'png', 'jpeg');
		// 设置附件上传类型
		$upload -> savePath = 'Uploads/' . date("Y/m/d/", time());
		// 设置附件上传目录
		FS::s_mkdir($upload -> savePath);
		if (!$upload -> upload()) {// 上传错误提示错误信息
			$msg = $upload -> getErrorMsg();
			$result['status'] = 0;
			$result['msg'] = $msg;
		} else {// 上传成功
			$imginfo = $upload -> getUploadFileInfo();
			$result['status'] = 1;
			$result['msg'] = $imginfo;
		}
		return $result;
	}
	/**
	$url，基准网址，若为空，将会自动获取，不建议设置为空
	$total，信息总条数
	$listRows，每页显示行数
	$pagebarnum，分页栏每页显示的页数
	$mode，显示风格，参数可为整数1，2，3，4任意一个
	 */
	protected function page($total=0,$listRows=10,$url="",$pageBarNum=10,$mode=1){
		$page=new Page();
		$page->pageSuffix=cpConfig::get('URL_HTML_SUFFIX');
		$cur_page=$page->getCurPage();//当前页码
		$limit_start=($cur_page-1)*$listRows;
		$limit=$limit_start.','.$listRows;
		$pagestring = $page->show($url,$total,$listRows,$pageBarNum,$mode) ;
		return array($limit,$pagestring);
	}
	
}
 //crul数据提交
function post_curl($data,$apiUrl){

/*		$db = model();
		$sync = $db->table('member_field')->where('uid='.$_SESSION['uid'])->find();		
		$url=$sync['client'].cpConfig::get($apiUrl);
		$url = 'http://124.128.61.235:9600/RESTService/'.cpConfig::get($apiUrl);*/
		$url = cpConfig::get('X3_API_URL').cpConfig::get($apiUrl);
		$timeout = 5;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		$result = curl_exec($ch);
		curl_close($ch);
		
		return json_decode($result,true);//返回数组
}
//curl获取数据
function get_curl($apiUrl){
	$url = cpConfig::get('X3_API_URL').cpConfig::get($apiUrl);
	$timeout = 5;
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt ($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	$result = curl_exec($ch);
	curl_close($ch);
	
	return json_decode($result,true);
}

